'-------------------------------------------------------------------------------
' Script to change the configuration URLs of the liquiddiagrams gadgets
' Replaces any occurence of a search string by another (recursively in subdirs).
' The URLs are configured in SEARCH_TEXT or REPLACE_TEXT.
'
' Author: Michael Krisper (michael.krisper@student.tugraz.at)
' Date: 28.8.2012
'-------------------------------------------------------------------------------
SEARCH_TEXT = "https://code.google.com/p/bakk/source/browse/gadgets"
'REPLACE_TEXT = "http://liquiddiagrams.com/gadgets/"
REPLACE_TEXT = "https://bakk.googlecode.com/git/gadgets"

Set objFSO = CreateObject("Scripting.FileSystemObject")
fileList = WalkSubfolders(objFSO.GetFolder("."))
If fileList = "" Then
	WScript.Echo "No files changed."
Else
	fileList = Replace(fileList, objFSO.GetFolder(".").Path, ".")
	WScript.Echo "Changed " & UBound(Split(fileList, ".\")) & " Files:" & vbCRLF & fileList
End If

Function WalkSubFolders(Folder)
    fileList = ""
    For Each Subfolder in Folder.SubFolders
        Set objFolder = objFSO.GetFolder(Subfolder.Path)
        Set colFiles = objFolder.Files
        For Each objFile in colFiles
			if ReplaceStrings(Subfolder.Path & "\" & objFile.Name) Then
				fileList = fileList & vbCRLF & Subfolder.Path & "\" & objFile.Name
			End If
        Next
		subFolderList = WalkSubFolders(Subfolder)
		If subFolderList <> "" Then
			fileList = fileList & vbCRLF & subFolderList
		End If
    Next
	WalkSubFolders = fileList
End Function

Function ReplaceStrings(fileName)
	ReplaceStrings = False
	Set oFSO = CreateObject("Scripting.FileSystemObject")

	If Not oFSO.FileExists(fileName) Then
		WScript.Echo "Specified file does not exist."
	Else
		Set oFile = oFSO.OpenTextFile(fileName, 1)
		strText = oFile.ReadAll
		oFile.Close
		
		changedText = Replace(strText, SEARCH_TEXT, REPLACE_TEXT)
		If changedText <> strText Then
			Set oFile = oFSO.OpenTextFile(fileName, 2)
			oFile.Write changedText
			oFile.Close
			ReplaceStrings = True
		End If
	End If
End Function
